package com.vvlive.caibao.smartcity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.bean.Person;

import org.kymjs.kjframe.KJBitmap;

import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/8/7.
 */
public class FriendsAdapter extends BaseAdapter {

    private final static int ITEM_LETTER = 0;
    private final static int ITEM_PERSON = 1;

    private Context mContext;
    private List<Object> objects;
    private KJBitmap mKjBitmap;
    private LayoutInflater inflater;

    public FriendsAdapter(Context context, List<Object> persons) {
        mContext = context;
        objects = persons;
        mKjBitmap = new KJBitmap();
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position) instanceof String ? ITEM_LETTER : ITEM_PERSON;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {

            holder = new ViewHolder();

            switch (type) {
                case ITEM_LETTER:
                    convertView = inflater.inflate(R.layout.item_pager_contact_friends_letter, null);
                    holder.letter = (TextView) convertView.findViewById(R.id.name_letter);
                    break;
                case ITEM_PERSON:
                    convertView = inflater.inflate(R.layout.item_pager_contact_friends_list, null);
                    holder.avatar = (ImageView) convertView.findViewById(R.id.friends_avatar);
                    holder.name = (TextView) convertView.findViewById(R.id.friends_name);
                    holder.sign = (TextView) convertView.findViewById(R.id.friends_sign);
                    break;
            }

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        switch (type) {
            case ITEM_LETTER:
                holder.letter.setText(getItem(position).toString());
                break;
            case ITEM_PERSON:
                Person person = (Person) getItem(position);
                mKjBitmap.display(holder.avatar, person.getPortrait());
                holder.name.setText(person.getName());
                holder.sign.setText(person.getNumber());
                break;
        }

        return convertView;
    }

    private class ViewHolder {
        public TextView letter;

        public ImageView avatar;
        public TextView name;
        public TextView sign;
    }
}
